import Vue from 'vue';
import Vuex from 'vuex';
import {
  setData,
  resultField,
  newLotteryField,
  listField
} from '@/helper/index';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    config: {
      name: '红包抽奖',
      number: 100,
      redPacket: 1000
    },
    result: {
      redPacket: []
    },
    newLottery: [],
    list: [
      { key: 3, name: '魏利民' },
      { key: 9, name: '郑俊强' },
      { key: 30, name: '于泳涛' },
      { key: 36, name: '赵菊红' },
      { key: 37, name: '黄志琴' },
      { key: 41, name: '张亚' },
      { key: 45, name: '李莎' },
      { key: 47, name: '陈磊' },
      { key: 48, name: '储君' },
      { key: 49, name: '范群' },
      { key: 50, name: '金星' },
      { key: 51, name: '宋向玉' },
      { key: 60, name: '周爱成' }, //no
      { key: 61, name: '杜金梅' }, //no
      { key: 62, name: '廖腾杰' }, //no
      { key: 63, name: '程丹' }, //no
      { key: 64, name: '温莉娟' }, //no
      { key: 65, name: '陶文文' }, //no
      { key: 1, name: '王玉梁' }, //no
      { key: 2, name: '陈克祥' } //no
    ],
    photos: []
  },
  mutations: {
    setClearConfig(state) {
      state.config = {
        name: '红包抽奖',
        number: 70,
        redPacket: 1
      };
      state.newLottery = [];
    },
    setClearList(state) {
      state.list = [];
    },
    setClearPhotos(state) {
      state.photos = [];
    },
    setClearResult(state) {
      state.result = {
        redPacket: []
      };
    },
    setClearStore(state) {
      state.config = {
        name: '红包抽奖',
        number: 70,
        redPacket: 1
      };
      state.result = {
        redPacket: []
      };
      state.newLottery = [];
      state.list = [];
      state.photos = [];
    },
    setConfig(state, config) {
      state.config = config;
    },
    setResult(state, result = {}) {
      state.result = result;

      setData(resultField, state.result);
    },
    setNewLottery(state, newLottery) {
      if (state.newLottery.find(item => item.name === newLottery.name)) {
        return;
      }
      state.newLottery.push(newLottery);
      setData(newLotteryField, state.newLottery);
    },
    setList(state, list) {
      const arr = state.list;
      list.forEach(item => {
        const arrIndex = arr.findIndex(data => data.key === item.key);
        if (arrIndex > -1) {
          arr[arrIndex].name = item.name;
        } else {
          arr.push(item);
        }
      });
      state.list = arr;

      setData(listField, arr);
    },
    setPhotos(state, photos) {
      state.photos = photos;
    }
  },
  actions: {},
  modules: {}
});
